package lsl.leetCode.test0722;

import java.util.Arrays;
import java.util.HashMap;

/**
 * 力扣 1
 *
 * description:两数之和
 * 给定一个整数数组 nums 和一个整数目标值 target，请你在该数组中找出和为目标值target的那两个整数，并返回它们的数组下标
 *
 *
 * @author liushengli
 * data: 2022/7/22
 */
public class test01 {
    public static void main(String[] args) {
        int[] nums = {4, 9, 11, 16, 19, 35};
        int target = 25;
        System.out.println(Arrays.toString(twoSum(nums, target)));
    }

    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[]{map.get(target - nums[i]), i};
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }
}
